package com.pxxy.cels.dao.impl;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.pxxy.cels.dao.NoteBookDao;
import com.pxxy.cels.entity.NoteBook;

/**
 * @program: Cels
 * @package: qikran.cels.dao.impl
 * @description: ${NoteBookDao接口实现}
 * @author: qi_kran
 * @create: 2020-04-07 22:29
 **/
public class NoteBookImpl implements NoteBookDao {
	
	@Override
	public void writeToNoteBook(List<NoteBook> list, File file) {
		ArrayList<NoteBook> arrayList = new ArrayList<>(list);
		try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file))) {
			oos.writeObject(arrayList);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<NoteBook> readFromNoteBook(File file) throws IOException {
		if (!file.exists()) {
			throw new IOException("文件不存在");
		}
		ArrayList<NoteBook> arrayList = new ArrayList<>();
		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
			arrayList.addAll((Collection<NoteBook>) ois.readObject());
		} catch (IOException | ClassNotFoundException e) {
			e.printStackTrace();
		}
		return arrayList;
	}
}
